//
//  CommunityInteractionViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/16.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "CommunityInteractionViewController.h"

#import "RadioButtonsView.h"
#import "CommunityInteractionTableViewCell.h"
#import "CommunityInteractionDetailViewController.h"
#import "InputView.h"
#import "StudentsEnjoyableAddViewController.h"

#define CommunityInteractionTableViewCellIdentifier @"communityInteractionTableViewCellIdentifier"

@interface CommunityInteractionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) RadioButtonsView * radioButtonsView; // 单行按钮组
@property (nonatomic, strong) UIButton * releaseButton; // 发布尊享按钮
@property (nonatomic, strong) UITableView * communityInteractionTableView; // 互动社区表格视图
@property (nonatomic, strong) NSMutableDictionary * parameters; // 网络请求参数
@property (nonatomic, assign) NSInteger pageNum; // 记录列表页码
@property (nonatomic, strong) NSMutableArray * dataSource; // 网络请求下来的数据
@property (nonatomic, strong) NSArray * groupList; // 邀请人员

@end

@implementation CommunityInteractionViewController

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 添加子视图
    [self.view addSubview:self.radioButtonsView];
    [self.view addSubview:self.releaseButton];
    [self.view addSubview:self.communityInteractionTableView];
    
    // 自动布局
    [_radioButtonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH * 0.08 * 2 + SCREEN_WIDTH * 0.02, SCREEN_WIDTH * 0.08 * 0.5));
        make.centerX.equalTo(self.view);
        make.top.offset(SCREEN_HEIGHT * 0.113);
    }];
    [_releaseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(_radioButtonsView);
        make.width.offset(SCREEN_WIDTH * 0.18);
        make.centerY.equalTo(_radioButtonsView);
        make.right.offset(SCREEN_WIDTH * -0.03);
    }];
    [_communityInteractionTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.top.equalTo(_releaseButton.mas_bottom);
        make.centerX.equalTo(self.view);
        make.bottom.offset(SCREEN_HEIGHT * -0.09);
    }];
    
    // 添加刷新
    [self addRefresh];
    // 注册单元格
    [self.communityInteractionTableView registerClass:[CommunityInteractionTableViewCell class] forCellReuseIdentifier:CommunityInteractionTableViewCellIdentifier];
    // 设置单元格高度
    _communityInteractionTableView.rowHeight = SCREEN_HEIGHT * 0.26;
    
    // 第一次网络请求
    self.parameters[@"read"] = _radioButtonsView.selecIndex == 10 ? @"all" : @"self";
    _pageNum = 0;
    [self loadData];
}

#pragma mark 🌷🌷🌷UITableViewDataSource🌷🌷🌷
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommunityInteractionTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CommunityInteractionTableViewCellIdentifier forIndexPath:indexPath];
    
    // cell 赋值
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND(_dataSource[indexPath.row][@"userPic"])] placeholderImage:[UIImage imageNamed:@"default_head_s.png"]];
    cell.titleLabel.text = _dataSource[indexPath.row][@"title"];
    cell.contentLabel.text = [NSString getEmojiStringForString:_dataSource[indexPath.row][@"desce"]];
    cell.nameLabel.text = [NSString stringWithFormat:@"昵称：%@", _dataSource[indexPath.row][@"nickName"]];
    cell.releaseDateLabel.text = [NSString stringWithFormat:@"发布时间：%@", _dataSource[indexPath.row][@"createTime"]];
    
    return cell;
}

#pragma mark 🌷🌷🌷UITableViewDelegate🌷🌷🌷
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 添加详细视图与控制器
    CommunityInteractionDetailViewController * communityInteractionDetailVc = [[CommunityInteractionDetailViewController alloc] init];
    communityInteractionDetailVc.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    // 属性传值
    communityInteractionDetailVc.id = _dataSource[indexPath.row][@"id"];
    [communityInteractionDetailVc.headImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND(_dataSource[indexPath.row][@"userPic"])] placeholderImage:[UIImage imageNamed:@"default_head_s.png"]];
    communityInteractionDetailVc.titleLabel.text = _dataSource[indexPath.row][@"title"];
    communityInteractionDetailVc.contentLabel.text = [NSString getEmojiStringForString:_dataSource[indexPath.row][@"desce"]];
    communityInteractionDetailVc.nameLabel.text = [NSString stringWithFormat:@"昵称：%@", _dataSource[indexPath.row][@"nickName"]];
    InputView * inputView = communityInteractionDetailVc.inputView;
    inputView.inputTextField.text = [NSString stringWithFormat:@"@%@:", _dataSource[indexPath.row][@"nickName"]];
    communityInteractionDetailVc.releaseDateLabel.text = [NSString stringWithFormat:@"发布时间：%@", _dataSource[indexPath.row][@"createTime"]];
    
    
    [self addChildViewController:communityInteractionDetailVc];
    [self.view addSubview:communityInteractionDetailVc.view];
    // 动画
    [UIView animateWithDuration:0.5 animations:^{
        communityInteractionDetailVc.view.center = CGPointMake(SCREEN_WIDTH * 0.5, SCREEN_HEIGHT * 0.5);
    }];
}

#pragma mark 🌷🌷🌷event response🌷🌷🌷
- (void)respondsToReleaseBtn:(UIButton *)sender {
    // 添加发布尊享视图与控制器
    StudentsEnjoyableAddViewController * studentsEnjoyableAddVc = [[StudentsEnjoyableAddViewController alloc] init];
    [self addChildViewController:studentsEnjoyableAddVc];
    [self.view addSubview:studentsEnjoyableAddVc.view];
}

#pragma mark 🌷🌷🌷private methods🌷🌷🌷
- (void)addRefresh {
    _communityInteractionTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 下拉刷新
        self.parameters[@"flag"] = _radioButtonsView.selecIndex == 10 ? @"all" : @"self";
        _pageNum = 0;
        [self.dataSource removeAllObjects];
        [self loadData];
    }];
    _communityInteractionTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 下拉加载更多
        if (_dataSource.count < (_pageNum + 1) * 10) {
            [MBProgressHUD showError:@"没有更多数据！"];
            [_communityInteractionTableView.mj_footer endRefreshing];
            return ;
        }
        _pageNum += 1;
        [self loadData];
    }];
}
- (void)loadData {
    self.parameters[@"pageNum"] = @(_pageNum);
    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"加载中..." url:BASEURLAPPEND(COMMUNITYINTERACTION_URL) parameters:_parameters finish:^(id responseObject, NSError *error) {
        NSLog(@"%@", responseObject);
        if (!error) {
            [MBProgressHUD showSuccess:@"加载完成！"];
            [self.dataSource addObjectsFromArray:responseObject[@"data"]];
            _groupList = responseObject[@"groupList"];
            [_communityInteractionTableView reloadData];
        } else {
            [MBProgressHUD showError:[[NSString stringWithFormat:@"%@", error] containsString:@"The request timed out."] ? @"登录超时" : @"I don't know"];
        }
        [_communityInteractionTableView.mj_header endRefreshing];
        [_communityInteractionTableView.mj_footer endRefreshing];
    }];
}

#pragma mark 🌷🌷🌷event response🌷🌷🌷
- (void)respondsToCommunityInteractionReleaseBtn:(UIButton *)sender {
    // 添加发布尊享视图与控制器
    StudentsEnjoyableAddViewController * studentsEnjoyableAddVc = [[StudentsEnjoyableAddViewController alloc] init];
    studentsEnjoyableAddVc.type = 1;
    studentsEnjoyableAddVc.dataSource = _groupList;
    [self addChildViewController:studentsEnjoyableAddVc];
    [self.view addSubview:studentsEnjoyableAddVc.view];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (RadioButtonsView *)radioButtonsView {
    if (!_radioButtonsView) {
        NSArray * titleNames = @[@"所有", @"个人"];
        _radioButtonsView = [[RadioButtonsView alloc] initWithNames:titleNames selectedIndex:^(NSInteger selectIndex) {
            self.parameters[@"flag"] = selectIndex == 0 ? @"all" : @"self";
            [_communityInteractionTableView.mj_header beginRefreshing];
        }];
    }
    return _radioButtonsView;
}
- (UIButton *)releaseButton {
    if (!_releaseButton) {
        _releaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _releaseButton.backgroundColor = RGB_COLOR(1, 157, 173, 1);
        [_releaseButton setTitle:@"+我要发布" forState:UIControlStateNormal];
        _releaseButton.titleLabel.font = [UIFont systemFontOfSize:25];
        [_releaseButton addTarget:self action:@selector(respondsToCommunityInteractionReleaseBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _releaseButton;
}
- (UITableView *)communityInteractionTableView {
    if (!_communityInteractionTableView) {
        _communityInteractionTableView = [[UITableView alloc] init];
        // 设置数据源
        _communityInteractionTableView.dataSource = self;
        // 设置代理
        _communityInteractionTableView.delegate = self;
        // 去掉分隔线
        _communityInteractionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _communityInteractionTableView;
}
- (NSMutableDictionary *)parameters {
    if (!_parameters) {
        _parameters = [NSMutableDictionary dictionaryWithObjects:@[@"all", [LoginUserInfo defaultUserInfo].userid, @(0), @(10)] forKeys:@[@"flag", @"id", @"pageNum", @"pagesize"]];
    }
    return _parameters;
}

@end
